<template>
	<view class="app-bg">

		<!-- <cu-custom bgColor="bg-gradual-primary" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">编辑</block>
		</cu-custom> -->

		<view style="padding: 15rpx;">
			<!-- 插件地址：https://ext.dcloud.net.cn/plugin?id=651 -->
			<view class="flex calendar-remark justify-between align-center padding-xs">
				<view class=" text-gray">开始时间:</view>
				<dyDatePicker placeholder="开始时间" :childValue="from" :minSelect="from_minSelect" :maxSelect="from_maxSelect"
				 :iconshow="false" @getData="getFromData"></dyDatePicker>
			</view>
			<view class="flex calendar-remark align-center justify-between padding-xs">
				<view class=" text-gray">结束时间:</view>
				<dyDatePicker placeholder="结束时间" :minSelect="to_minSelect" :childValue="to" :maxSelect="to_maxSelect" :iconshow="true"
				 @getData="getToData"></dyDatePicker>
			</view>

			<view style="height: 300rpx;"></view>

			<button class="flex cu-btn round justify-center margin-xl bg-primary" @click="saveClick">
				保存
			</button>

			<button v-if="showDelete" class="flex cu-btn round line-primary justify-center margin-xl" @click="deleteClick">
				删除
			</button>
		</view>
	</view>
</template>

<script>
	import dyDatePicker from "../../../components/dy-Date/dy-Date.vue";
	import helper from '../../../common/helper.js';
	import util from '../../../common/util.js';
	import Bus from '../../../common/bus.vue';
	export default {
		components: {
			dyDatePicker
		},
		data() {
			return {
				from_minSelect: '1900/01/01',
				from_maxSelect: '2050/12/31',
				to_minSelect: '1900/01/01',
				to_maxSelect: '2050/12/31',
				from: '',
				to: '',
				myClicItemData: {},
				pageFrom: '',
				showDelete: false,
			};
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log(option.from)

			//区分是从哪来的
			this.pageFrom = option.from
			if (option.from === 'myItemClick') {
				this.showDelete = true;
				this.myClicItemData = getApp().globalData.myClicItemData;
				this.from = this.myClicItemData.ymStartTime.replace(/-/g, '/');
				if (this.myClicItemData.ymEndTime) {
					this.to = this.myClicItemData.ymEndTime.replace(/-/g, '/');
				}
				console.log(getApp().globalData.myClicItemData)
			}


		},
		methods: {
			onShareAppMessage: function() {
				qq.showShareMenu({
					showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
				})
			},
			/**
			 * @param {to_minSelect|from}  to_minSelect结束时间的最小选择范围 from=>开始日期
			 */
			getFromData(time) {
				this.to_minSelect = time
				this.from = time
			},
			/**
			 * @param {from_maxSelect|to} from_maxSelect=>开始日期可选最大可选值 to=> 结束日期
			 */
			getToData(time) {
				this.from_maxSelect = time
				this.to = time
			},
			getData() {
				// 
			},

			saveClick() {
				this.addOrUpdate();
			},

			deleteClick() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除吗？',
					success: function(res) {
						if (res.confirm) {
							that.delete();
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},


			// 调用服务器
			addOrUpdate() {

				let that = this;

				let userInfo = uni.getStorageSync('userInfo');
				console.log(userInfo);

				let ymStartTime = that.from.replace('/', '-').replace('/', '-');
				let ymEndTime = that.to.replace('/', '-').replace('/', '-');

				let playod = {
					"id": that.myClicItemData.id,
					"userId": userInfo.userId,
					"ymStartTime": ymStartTime,
					"ymEndTime": ymEndTime
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/addOrUpdate', //仅为示例，并非真实接口地址。
					method: 'post',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},

					success: (res) => {
						uni.hideLoading();
						console.log(res.data);

						if (res.data.code === 0) {
							uni.showToast({
								icon: "success",
								title: "操作成功！",
								duration: 1000
							});
							setTimeout(function() {

								Bus.$emit('recordChange', "fromYmDetail");


							}, 1000);


							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			},

			// 调用服务器
			delete() {

				let that = this;
				let playod = {
					"id": that.myClicItemData.id
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/removeById', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},

					success: (res) => {
						uni.hideLoading();
						console.log(res.data);
						Bus.$emit('recordChange', "fromYmDetail");

						if (res.data.code === 0) {
							// uni.showToast({
							// 	title:"操作成功！",
							// 	duration:1000
							// });
							uni.navigateBack({
								delta: 1
							});


							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			}

		}

	}
</script>

<style lang="scss">
	.calendar-remark {
		width: 100%;
		padding: 15rpx;
		border: 1px solid;
		margin-top: 50rpx;
		border-radius: 5px;
		border-color: $uni-color-primary;
		color: $uni-text-color;

	}
</style>
